/*
 * Copyright (c) 2023 iQi.Co.Ltd. All rights reserved.
 */
 
using System.Collections.Generic;
using NetProtocol.POD;
using NetProtocol.Enum;
using IQIGame.Onigao.Game;
using IQIGame.Onigao.GamePlay;
using IQIGame.Onigao.Framework;
using Cysharp.Threading.Tasks;
using System;
using static IQIGame.Onigao.Game.MessageWrapper;

namespace NetProtocol.Client {
	/// <summary>
	/// 中心服聊天模块
	/// module : centerChat
	/// </summary>
	public interface CNetCenterChat : INetClientModule {
		
		public ISession GetSession();
	
		Dictionary<int, INetClientModule.Package2MessageWrapper> INetClientModule.GetModuleFunction()
        {
            MessageFunction sendChatResult_Delegate = (session, wrapper) =>
            {
            	var _wrapper = (MessageWrapper<SendChatResultData>)wrapper;
                SC_sendChatResult(session, ref _wrapper.message);
                MessageWrapperPool<SendChatResultData>.Put(_wrapper);
            };
            MessageFunction switchRoomResult_Delegate = (session, wrapper) =>
            {
            	var _wrapper = (MessageWrapper<SwitchRoomResultData>)wrapper;
                SC_switchRoomResult(session, ref _wrapper.message);
                MessageWrapperPool<SwitchRoomResultData>.Put(_wrapper);
            };
            MessageFunction notifyChat_Delegate = (session, wrapper) =>
            {
            var _wrapper = (MessageWrapper<NotifyChatData>)wrapper; 
#if ENABLE_NET_LOG || UNITY_EDITOR
            NetProtocolHelper.ClientLog(NetMessageId.centerChat_notifyChat, "Recv->" + _wrapper.message.ToString());
#endif
                SC_notifyChat(session, _wrapper.message.chat);
                MessageWrapperPool<NotifyChatData>.Put(_wrapper);
            };
            MessageFunction notifyChatRoom_Delegate = (session, wrapper) =>
            {
            var _wrapper = (MessageWrapper<NotifyChatRoomData>)wrapper; 
#if ENABLE_NET_LOG || UNITY_EDITOR
            NetProtocolHelper.ClientLog(NetMessageId.centerChat_notifyChatRoom, "Recv->" + _wrapper.message.ToString());
#endif
                SC_notifyChatRoom(session, _wrapper.message.chatRoom);
                MessageWrapperPool<NotifyChatRoomData>.Put(_wrapper);
            };
            MessageFunction reportPlayerResult_Delegate = (session, wrapper) =>
            {
            	var _wrapper = (MessageWrapper<ReportPlayerResultData>)wrapper;
                SC_reportPlayerResult(session, ref _wrapper.message);
                MessageWrapperPool<ReportPlayerResultData>.Put(_wrapper);
            };
        
            Dictionary<int, INetClientModule.Package2MessageWrapper> retDic = new()
            {
                {NetMessageId.centerChat_sendChatResult, (session, package) => {
                	var result = MessageWrapperPool<SendChatResultData>.Get();
                	result.Init(session, sendChatResult_Delegate);
                	result.FromPackage(package);
                	return result;
                }},
                {NetMessageId.centerChat_switchRoomResult, (session, package) => {
                	var result = MessageWrapperPool<SwitchRoomResultData>.Get();
                	result.Init(session, switchRoomResult_Delegate);
                	result.FromPackage(package);
                	return result;
                }},
                {NetMessageId.centerChat_notifyChat, (session, package) => {
                	var result = MessageWrapperPool<NotifyChatData>.Get();
                	result.Init(session, notifyChat_Delegate);
                	result.FromPackage(package);
                	return result;
                }},
                {NetMessageId.centerChat_notifyChatRoom, (session, package) => {
                	var result = MessageWrapperPool<NotifyChatRoomData>.Get();
                	result.Init(session, notifyChatRoom_Delegate);
                	result.FromPackage(package);
                	return result;
                }},
                {NetMessageId.centerChat_reportPlayerResult, (session, package) => {
                	var result = MessageWrapperPool<ReportPlayerResultData>.Get();
                	result.Init(session, reportPlayerResult_Delegate);
                	result.FromPackage(package);
                	return result;
                }},
            };
            return retDic;
        }
#region Client-side Interface
		/// <summary>
        /// 
        /// </summary>
		public void SC_sendChatResult(ISession session, ref SendChatResultData message) 
		{
            _SC_sendChatResult(session, ref message);
		}
		
		public void _SC_sendChatResult(ISession session, ref SendChatResultData message)
		{
#if ENABLE_NET_LOG || UNITY_EDITOR
            NetProtocolHelper.ClientLog(NetMessageId.centerChat_sendChatResult, "Recv->" + message.ToString());
#endif
			NetProtocolHelper.endCallRequest?.Invoke(NetMessageId.centerChat_sendChatResult, message.code);
			MsgAsyncAwaiter<SendChatResultData>.Instance.CompleteMsgAwait(ref message);
		}
		
		/// <summary>
        /// 
        /// </summary>
		public void SC_switchRoomResult(ISession session, ref SwitchRoomResultData message) 
		{
            _SC_switchRoomResult(session, ref message);
		}
		
		public void _SC_switchRoomResult(ISession session, ref SwitchRoomResultData message)
		{
#if ENABLE_NET_LOG || UNITY_EDITOR
            NetProtocolHelper.ClientLog(NetMessageId.centerChat_switchRoomResult, "Recv->" + message.ToString());
#endif
			NetProtocolHelper.endCallRequest?.Invoke(NetMessageId.centerChat_switchRoomResult, message.code);
			MsgAsyncAwaiter<SwitchRoomResultData>.Instance.CompleteMsgAwait(ref message);
		}
		
		/// <summary>
        ///  通知聊天信息
        /// </summary>
		protected void SC_notifyChat(ISession session, SChatPOD chat);
		/// <summary>
        ///  通知聊天室更新
        /// </summary>
		protected void SC_notifyChatRoom(ISession session, ChatRoomPOD chatRoom);
		/// <summary>
        /// 
        /// </summary>
		public void SC_reportPlayerResult(ISession session, ref ReportPlayerResultData message) 
		{
            _SC_reportPlayerResult(session, ref message);
		}
		
		public void _SC_reportPlayerResult(ISession session, ref ReportPlayerResultData message)
		{
#if ENABLE_NET_LOG || UNITY_EDITOR
            NetProtocolHelper.ClientLog(NetMessageId.centerChat_reportPlayerResult, "Recv->" + message.ToString());
#endif
			NetProtocolHelper.endCallRequest?.Invoke(NetMessageId.centerChat_reportPlayerResult, message.code);
			MsgAsyncAwaiter<ReportPlayerResultData>.Instance.CompleteMsgAwait(ref message);
		}
		
#endregion
#region Server-side Interface
		/// <summary>
        ///  发送聊天
        /// </summary>
    	public UniTask<SendChatResultData> CS_sendChat(CChatPOD chat)
        {
            return _CS_sendChat(chat);
        }
        
        public UniTask<SendChatResultData> _CS_sendChat(CChatPOD chat)
        {
        	ISession session = GetSession();
        	if(session == null) return UniTask.FromResult(default(SendChatResultData));
#if ENABLE_NET_LOG || UNITY_EDITOR
			NetProtocolHelper.ClientLog(NetMessageId.centerChat_sendChat, "Send->"+"CenterChat.CS_sendChat { "
				+ "chat="
				+ chat
				+" }");
#endif
        	UniTask<SendChatResultData> task = MsgAsyncAwaiter<SendChatResultData>.Instance.AddMsgAwait();
        	session.SendPack(Pack_for_sendChat(chat));
        	NetProtocolHelper.beginCallRequest?.Invoke(NetMessageId.centerChat_sendChatResult);
			return task;
        }
		
		public static SendPackage Pack_for_sendChat(CChatPOD chat) 
		{
			SendPackage si_p = NetProtocolHelper.GetSendPackage();
			si_p.Header.Messagid = NetMessageId.centerChat_sendChat;
			si_p.StartWrite();
			SOutputStream _out = si_p.OutputStream;
			_out.Write_pod(chat);
			return si_p;
		}
		/// <summary>
        ///  切换房间
        /// </summary>
    	public UniTask<SwitchRoomResultData> CS_switchRoom(int roomId)
        {
            return _CS_switchRoom(roomId);
        }
        
        public UniTask<SwitchRoomResultData> _CS_switchRoom(int roomId)
        {
        	ISession session = GetSession();
        	if(session == null) return UniTask.FromResult(default(SwitchRoomResultData));
#if ENABLE_NET_LOG || UNITY_EDITOR
			NetProtocolHelper.ClientLog(NetMessageId.centerChat_switchRoom, "Send->"+"CenterChat.CS_switchRoom { "
				+ "roomId="
				+ roomId
				+" }");
#endif
        	UniTask<SwitchRoomResultData> task = MsgAsyncAwaiter<SwitchRoomResultData>.Instance.AddMsgAwait();
        	session.SendPack(Pack_for_switchRoom(roomId));
        	NetProtocolHelper.beginCallRequest?.Invoke(NetMessageId.centerChat_switchRoomResult);
			return task;
        }
		
		public static SendPackage Pack_for_switchRoom(int roomId) 
		{
			SendPackage si_p = NetProtocolHelper.GetSendPackage();
			si_p.Header.Messagid = NetMessageId.centerChat_switchRoom;
			si_p.StartWrite();
			SOutputStream _out = si_p.OutputStream;
			_out.Write_int(roomId);
			return si_p;
		}
		/// <summary>
        /// 举报玩家(type:聊天类型(聊天频道,比如世界,私聊) , text:举报内容(被举报人发言内容) reason:举报原因(举报分类名称，如：发布广告。多个用英文分号分割), remark:举报人备注内容)
        /// </summary>
    	public UniTask<ReportPlayerResultData> CS_reportPlayer(long pid, int type, string text, string reason, string remark)
        {
            return _CS_reportPlayer(pid, type, text, reason, remark);
        }
        
        public UniTask<ReportPlayerResultData> _CS_reportPlayer(long pid, int type, string text, string reason, string remark)
        {
        	ISession session = GetSession();
        	if(session == null) return UniTask.FromResult(default(ReportPlayerResultData));
#if ENABLE_NET_LOG || UNITY_EDITOR
			NetProtocolHelper.ClientLog(NetMessageId.centerChat_reportPlayer, "Send->"+"CenterChat.CS_reportPlayer { "
				+ "pid="
				+ pid
				+ ", type="
				+ type
				+ ", text="
				+ text
				+ ", reason="
				+ reason
				+ ", remark="
				+ remark
				+" }");
#endif
        	UniTask<ReportPlayerResultData> task = MsgAsyncAwaiter<ReportPlayerResultData>.Instance.AddMsgAwait();
        	session.SendPack(Pack_for_reportPlayer(pid, type, text, reason, remark));
        	NetProtocolHelper.beginCallRequest?.Invoke(NetMessageId.centerChat_reportPlayerResult);
			return task;
        }
		
		public static SendPackage Pack_for_reportPlayer(long pid, int type, string text, string reason, string remark) 
		{
			SendPackage si_p = NetProtocolHelper.GetSendPackage();
			si_p.Header.Messagid = NetMessageId.centerChat_reportPlayer;
			si_p.StartWrite();
			SOutputStream _out = si_p.OutputStream;
			_out.Write_long(pid);
			_out.Write_int(type);
			_out.Write_string(text);
			_out.Write_string(reason);
			_out.Write_string(remark);
			return si_p;
		}
#endregion
#region Messages
        public struct SendChatResultData : IReceiveMessage {
        	private int? _code;
        	public int code {get{return _code == null ? -1 : (int)_code;}private set{_code = value;}}
            public bool IsFail => _code != null && _code != 0;
            public bool IsSuccess =>_code != null && _code == 0;
            public bool IsCanceled => _code == null;
            public void FromPackage(ReceivePackage _package)
            {
                SInputStream _in = _package.InputStream;
                code = _in.HasRemaining() ? _in.Read_int() : default;
            }
            
            public int GetMessageId() {return NetMessageId.centerChat_sendChatResult;}
            
            public override string ToString()
            {
                return "CenterChat.sendChatResult"+ ", code="+ code;
            }
        }
        public struct SwitchRoomResultData : IReceiveMessage {
        	private int? _code;
        	public int code {get{return _code == null ? -1 : (int)_code;}private set{_code = value;}}
	        public ChatRoomPOD chatRoom;
            public bool IsFail => _code != null && _code != 0;
            public bool IsSuccess =>_code != null && _code == 0;
            public bool IsCanceled => _code == null;
            public void FromPackage(ReceivePackage _package)
            {
                SInputStream _in = _package.InputStream;
                code = _in.HasRemaining() ? _in.Read_int() : default;
                chatRoom = _in.HasRemaining() ? _in.Read_pod<ChatRoomPOD>(ChatRoomPOD.DynamicFactory) : default;
            }
            
            public int GetMessageId() {return NetMessageId.centerChat_switchRoomResult;}
            
            public override string ToString()
            {
                return "CenterChat.switchRoomResult"+ ", code="+ code+ ", chatRoom="+ chatRoom;
            }
        }
        public struct NotifyChatData : IReceiveMessage {
	        public SChatPOD chat;
            public void FromPackage(ReceivePackage _package)
            {
                SInputStream _in = _package.InputStream;
                chat = _in.HasRemaining() ? _in.Read_pod<SChatPOD>(SChatPOD.DynamicFactory) : default;
            }
            
            public int GetMessageId() {return NetMessageId.centerChat_notifyChat;}
            
            public override string ToString()
            {
                return "CenterChat.notifyChat"+ ", chat="+ chat;
            }
        }
        public struct NotifyChatRoomData : IReceiveMessage {
	        public ChatRoomPOD chatRoom;
            public void FromPackage(ReceivePackage _package)
            {
                SInputStream _in = _package.InputStream;
                chatRoom = _in.HasRemaining() ? _in.Read_pod<ChatRoomPOD>(ChatRoomPOD.DynamicFactory) : default;
            }
            
            public int GetMessageId() {return NetMessageId.centerChat_notifyChatRoom;}
            
            public override string ToString()
            {
                return "CenterChat.notifyChatRoom"+ ", chatRoom="+ chatRoom;
            }
        }
        public struct ReportPlayerResultData : IReceiveMessage {
        	private int? _code;
        	public int code {get{return _code == null ? -1 : (int)_code;}private set{_code = value;}}
            public bool IsFail => _code != null && _code != 0;
            public bool IsSuccess =>_code != null && _code == 0;
            public bool IsCanceled => _code == null;
            public void FromPackage(ReceivePackage _package)
            {
                SInputStream _in = _package.InputStream;
                code = _in.HasRemaining() ? _in.Read_int() : default;
            }
            
            public int GetMessageId() {return NetMessageId.centerChat_reportPlayerResult;}
            
            public override string ToString()
            {
                return "CenterChat.reportPlayerResult"+ ", code="+ code;
            }
        }
    }
#endregion
}